public static String toPostfix(String expr){
        MyStack<String> stack = new MyStack<>(expr.length());
        String postfix = "";
        int i = 0;
        while(i<expr.length()){
            char ch = expr.charAt(i);
            switch (ch){
                case  '+':
                case  '-':while(!stack.isEmpty() && !stack.get().equals("("))
                    postfix += stack.pop();
                    //postfix += " ";
                    stack.push(ch + "");
                    i++;
                    break;
                case '*':
                case '/':while (!stack.isEmpty() && (stack.get().equals("*")||stack.get().equals("/")))
                    postfix += stack.pop();
                    //postfix += " ";
                    stack.push(ch + "");
                    i++;
                    break;
                case '(':stack.push(ch + "");
                    i++;
                    break;
                case ')':String out = stack.pop();
                    while(out!=null && !out.equals("(")){
                        postfix += out;
                        out = stack.pop();
                        //postfix += " ";
                    }
                    i++;
                    break;
                default:while(i < expr.length() && ch>='0' && ch<='9'){
                    postfix += ch;
                    i++;
                    if(i<expr.length())
                        ch = expr.charAt(i);
                }
                postfix += " ";
            }
        }
        while (!stack.isEmpty())
            postfix += stack.pop();
        return postfix;
    }


public int value(String postfix){
        Stack<Integer> stack = new Stack();
        int i = 0, result = 0;
        while(i < postfix.length()){
            char ch = postfix.charAt(i);
            if(ch>='0' && ch<='9'){
                result = 0;
                while(ch!=' '){
                    result = result*10 + Integer.parseInt(ch+"");
                    i++;
                    ch = postfix.charAt(i);
                }
                i++;
                stack.push(new Integer(result));
            }
            else{
                int y = stack.pop().intValue();
                int x = stack.pop().intValue();
                switch (ch){
                    case ADD:
                        result = x + y;
                        break;
                    case SUBTRACT:
                        result = x - y;
                        break;
                    case MULTIPLY:
                        result = x * y;
                        break;
                    case DIVIDE:
                        result = x / y;
                }
                stack.push(new Integer(result));
                i++;
            }
        }
        return stack.pop().intValue();
    }

public class NewMyDCTester {
    public static void main(String [] args) {
        String expression;
        int result;
        try
        {
            Scanner in = new Scanner(System.in);
            NewMyDC evaluator = new NewMyDC();
            System.out.println ("Enter a valid postfix expression: ");
            expression = in.nextLine();
            String postfix = MyBC.toPostfix(expression);
            System.out.println ("The postfix expression is :" + postfix);
            result = evaluator.value (postfix);
            System.out.println ("That expression equals :" + result);
        }
        catch (Exception IOException)
        {
            System.out.println("Input exception reported");
        }
    }
}
